// utils/dialog.js
import { createApp, h } from 'vue'
import Dialog from '@/components/Common/Dialog/index.vue'

export function showDialog(options = {}) {
  const {
    title = '',
    message = '', // 注意：你之前用的是 content，但调用时传的是 message
    cancelText = '取消',
    confirmText = '确定'
  } = options

  // 使用 Promise 包装
  return new Promise((resolve, reject) => {
    const container = document.createElement('div')
    document.body.appendChild(container)

    let visible = true

    const destroy = () => {
      visible = false
      app.unmount()
      document.body.removeChild(container)
    }

    const app = createApp({
      render() {
        return h(Dialog, {
          title,
          content: message, // 注意这里：组件用的是 content，但外部传的是 message
          visible,
          cancelText,
          confirmText,
          onClose: () => {
            destroy()
            reject(new Error('dialog closed')) // 或 resolve(false)
          },
          onConfirm: () => {
            destroy()
            resolve(true)
          },
          onCancel: () => {
            destroy()
            reject(new Error('canceled')) // 或 resolve(false)
          }
        })
      }
    })

    app.mount(container)
  })
}